package li.nibble.twittertido;

import li.nibble.twittertido.model.DataBase;
import li.nibble.twittertido.webservice.Client;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

public class CreateAccount extends Activity {

	private boolean correct_user = false;
	private boolean correct_email = false;
	private boolean correct_password = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_user);

		final EditText username = (EditText) findViewById(R.id.username);
		final EditText email = (EditText) findViewById(R.id.email);
		final EditText password = (EditText) findViewById(R.id.password);
		final EditText confirm = (EditText) findViewById(R.id.comfirm);
		Button create = (Button) findViewById(R.id.create);

		username.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (!hasFocus) {
					TextView error_username = (TextView) findViewById(R.id.error_username);
					ProgressBar loading = (ProgressBar) findViewById(R.id.loading_username);
					loading.setVisibility(View.VISIBLE);
					new ValidateTask(getApplicationContext(), error_username,
							username, null, null, ValidateTask.USERNAME,
							loading).execute();
				}
			}
		});

		email.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (!hasFocus) {
					TextView error = (TextView) findViewById(R.id.error_email);
					ProgressBar loading = (ProgressBar) findViewById(R.id.loading_email);
					loading.setVisibility(View.VISIBLE);
					new ValidateTask(getApplicationContext(), error, email,
							null, null, ValidateTask.EMAIL, loading).execute();
				}
			}
		});

		password.setOnFocusChangeListener(new OnFocusChangeListener() {
			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (!hasFocus) {
					TextView error_email = (TextView) findViewById(R.id.error_password);
					if (password.getText().toString().length() > 0) {
						if (password.getText().toString().length() < 6) {
							error_email.setVisibility(View.VISIBLE);
							password.setTextColor(Color.parseColor("#ff7373"));
							correct_password = false;
						} else {
							error_email.setVisibility(View.GONE);
							password.setTextColor(Color.parseColor("#00c322"));
							correct_password = true;
						}
					} else {
						correct_password = false;
						password.setTextColor(Color.parseColor("#ff7373"));
					}
				}
			}
		});

		confirm.setOnEditorActionListener(new OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {
				TextView error_confirm = (TextView) findViewById(R.id.error_confirm);
				if (confirm.getText().toString().length() > 0) {
					if (password.getText().toString()
							.compareTo(confirm.getText().toString()) != 0) {
						error_confirm.setVisibility(View.VISIBLE);
						password.setTextColor(Color.parseColor("#ff7373"));
						correct_password = false;
					} else {
						error_confirm.setVisibility(View.GONE);
						password.setTextColor(Color.parseColor("#00c322"));
						correct_password = true;
					}
				} else {
					correct_password = false;
				}
				return false;
			}
		});

		create.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ProgressBar loading = (ProgressBar) findViewById(R.id.loading);
				RelativeLayout all_content = (RelativeLayout) findViewById(R.id.all_content);
				loading.setVisibility(View.VISIBLE);
				all_content.setVisibility(View.GONE);
				new ValidateTask(getApplicationContext(), null, username,
						email, password, ValidateTask.CREATE, loading).execute();
			}
		});
	}

	public class ValidateTask extends AsyncTask<Void, Void, Void> {

		Context context;
		ProgressBar loading;
		TextView error;
		EditText edittext;
		EditText edittext2;
		EditText edittext3;
		int tipo_validacion;
		public static final int USERNAME = 0;
		public static final int EMAIL = 1;
		public static final int PASSWORD = 2;
		public static final int CONFIRMATION = 3;
		public static final int CREATE = 4;

		public ValidateTask(Context context, TextView error, EditText edittext,
				EditText edittext2, EditText edittext3, int tipo_validacion,
				ProgressBar loading) {
			this.context = context;
			this.error = error;
			this.edittext = edittext;
			this.edittext2 = edittext2;
			this.edittext3 = edittext3;
			this.loading = loading;
			this.tipo_validacion = tipo_validacion;
		}

		@Override
		protected Void doInBackground(Void... params) {
			// Simulates a background job.
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				;
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void voids) {

			switch (tipo_validacion) {
			case USERNAME:
				if (edittext.length() > 0) {
					error.setVisibility(View.GONE);
					correct_password = false;
					if (Utils.isTextValid(edittext.getText().toString())) {
						if (Client.existUser(edittext.getText().toString())) {
							error.setText(context
									.getString(R.string.user_exists));
							error.setVisibility(View.VISIBLE);
							correct_user = false;
							edittext.setTextColor(Color.parseColor("#ff7373"));
						} else {
							error.setVisibility(View.GONE);
							edittext.setTextColor(Color.parseColor("#00c322"));
							correct_user = true;
						}
					} else {
						correct_password = false;
						error.setText(context
								.getString(R.string.error_username_invalid));
						error.setVisibility(View.VISIBLE);
						edittext.setTextColor(Color.parseColor("#ff7373"));
					}

				} else {
					correct_password = false;
				}
				loading.setVisibility(View.GONE);
				break;
			case EMAIL:
				if (edittext.getText().toString().length() > 0) {
					error.setVisibility(View.GONE);
					correct_email = false;
					if (Utils.isEmail(edittext.getText().toString())) {
						if (Client
								.existUserEmail(edittext.getText().toString())) {
							error.setText(context
									.getString(R.string.error_email));
							error.setVisibility(View.VISIBLE);
							edittext.setTextColor(Color.parseColor("#ff7373"));
							correct_email = false;
						} else {
							error.setVisibility(View.GONE);
							edittext.setTextColor(Color.parseColor("#00c322"));
							correct_email = true;
						}
					} else {
						error.setVisibility(View.VISIBLE);
						error.setText(context
								.getString(R.string.error_email_invalid));
						edittext.setTextColor(Color.parseColor("#ff7373"));
						correct_email = false;
					}

				} else {
					correct_password = false;
				}
				loading.setVisibility(View.GONE);
				break;
			case PASSWORD:
				break;
			case CONFIRMATION:
				break;
			case CREATE:
				register(edittext.getText().toString(), edittext2.getText()
						.toString(), edittext3.getText().toString());
				break;
			default:
				break;
			}

			// if (username.length() > 0 && password.length() > 0) {
			//
			// int result = Client.login(username, password);
			// if (result > 0) {
			// if (!DataBase.registered(context)) {
			// DataBase.localLogin(context, username, password,
			// result, Client.getEmail(username, password));
			// }
			// startActivity(new Intent(context, Main.class)
			// .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
			// } else {
			// error_text.setText(context.getText(R.string.error_login));
			// error_text.setVisibility(View.VISIBLE);
			// buttons.setVisibility(View.VISIBLE);
			// et_username.setVisibility(View.VISIBLE);
			// et_password.setVisibility(View.VISIBLE);
			// loading.setVisibility(View.GONE);
			// }
			//
			// }
		}

		public void register(String username, String email, String password) {
			if (correct_user && correct_email && correct_password) {
				int id = Client.createUser(username, email, password);
				if (id > 0) {
					DataBase.localLogin(context, username, password, id, email);
					startActivity(new Intent(context, Main.class)
							.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
				} else {
					Toast.makeText(context, "No ha podido crear el usuario",
							Toast.LENGTH_SHORT);
				}
			}
		}
	}

}
